﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using DTO;
using UTIL;

namespace DAO
{
    public class LoaiNguoiDungDao
    {
        public static bool ThemLoaiNguoiDung(LoaiNguoiDungDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_themLoaiNguoiDung");
                StoreUtilities.AddInputPara(cmd, "@loainguoidung", SqlDbType.NVarChar, one.LoaiNguoiDung);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
                throw ex;

            }
            return result;
        }

        public static bool xoaLoaiNguoiDung(LoaiNguoiDungDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_xoaLoaiNguoiDung");
                StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, one.ID);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
            }
            return result;
        }


        public static List<LoaiNguoiDungDto> layDanhSachLoaiNguoiDung()
        {
            SqlConnection cnn = DataConnector.MakeConnection();

            SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_layDanhSachLoaiNguoiDung");

            SqlDataReader dr = cmd.ExecuteReader();

            List<LoaiNguoiDungDto> danhsach = new List<LoaiNguoiDungDto>();
            LoaiNguoiDungDto loainguoidung = null;
            while (dr.Read())
            {
                loainguoidung = new LoaiNguoiDungDto();
                loainguoidung.ID = (int)dr["ID"];
                loainguoidung.LoaiNguoiDung = (String)dr["loainguoidung"];
                danhsach.Add(loainguoidung);
            }
            // dong ket noi
            cnn.Close();

            return danhsach;
        }
    }
}
